package com.smartcar.controller;

import com.smartcar.dto.ApiResponse;
import com.smartcar.dto.CarControlRequest;
import com.smartcar.enums.CarDirection;
import com.smartcar.service.CarApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 小车控制控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/car")
@CrossOrigin(origins = "*")
public class CarController {

    private final CarApiService carApiService;

    public CarController(CarApiService carApiService) {
        this.carApiService = carApiService;
    }

    /**
     * 按钮控制小车
     */
    @PostMapping("/control/button")
    public ApiResponse<Void> buttonControl(@RequestParam CarDirection direction) {
        try {
            carApiService.carBtnCtrl(direction);
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("按钮控制失败", e);
            return ApiResponse.error("按钮控制失败: " + e.getMessage());
        }
    }

    /**
     * 摇杆控制小车
     */
    @PostMapping("/control/joystick")
    public ApiResponse<Void> joystickControl(@Valid @RequestBody CarControlRequest request) {
        try {
            if (request.getSpeedX() != null && request.getSpeedY() != null) {
                carApiService.ctrlCar(request.getSpeedX(), request.getSpeedY());
                return ApiResponse.success();
            } else {
                return ApiResponse.error("速度参数不能为空");
            }
        } catch (Exception e) {
            log.error("摇杆控制失败", e);
            return ApiResponse.error("摇杆控制失败: " + e.getMessage());
        }
    }

    /**
     * 四轮单独控制
     */
    @PostMapping("/control/wheels")
    public ApiResponse<Void> wheelsControl(@Valid @RequestBody CarControlRequest request) {
        try {
            if (request.getL1() != null && request.getL2() != null && 
                request.getR1() != null && request.getR2() != null) {
                carApiService.upSpeedCar(request.getL1(), request.getL2(), request.getR1(), request.getR2());
                return ApiResponse.success();
            } else {
                return ApiResponse.error("轮子速度参数不能为空");
            }
        } catch (Exception e) {
            log.error("四轮控制失败", e);
            return ApiResponse.error("四轮控制失败: " + e.getMessage());
        }
    }

    /**
     * 开始循迹
     */
    @PostMapping("/tracking/start")
    public ApiResponse<Void> startTracking() {
        try {
            carApiService.startTracking();
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("开始循迹失败", e);
            return ApiResponse.error("开始循迹失败: " + e.getMessage());
        }
    }

    /**
     * 停止循迹
     */
    @PostMapping("/tracking/stop")
    public ApiResponse<Void> stopTracking() {
        try {
            carApiService.stopTracking();
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("停止循迹失败", e);
            return ApiResponse.error("停止循迹失败: " + e.getMessage());
        }
    }

    /**
     * 拍摄图片
     */
    @PostMapping("/camera/photo")
    public ApiResponse<Void> takePhoto() {
        try {
            carApiService.takePhoto();
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("拍摄图片失败", e);
            return ApiResponse.error("拍摄图片失败: " + e.getMessage());
        }
    }

    /**
     * 开始录像
     */
    @PostMapping("/camera/record/start")
    public ApiResponse<Void> startRecording() {
        try {
            carApiService.startRecording();
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("开始录像失败", e);
            return ApiResponse.error("开始录像失败: " + e.getMessage());
        }
    }

    /**
     * 停止录像
     */
    @PostMapping("/camera/record/stop")
    public ApiResponse<Void> stopRecording() {
        try {
            carApiService.stopRecording();
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("停止录像失败", e);
            return ApiResponse.error("停止录像失败: " + e.getMessage());
        }
    }
} 